package com.lijin.workshop.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lijin.workshop.dao.DataDicDAO;
import com.lijin.workshop.dao.mybatis.model.DataDic;
import com.lijin.workshop.entity.Pager;
import com.lijin.workshop.util.AssertUtil;

@Service(value="dataDicService")
public class DataDicService extends BaseService {

	@Autowired
	private DataDicDAO dataDicDAO;

	/**
	 * 分页查询数据字典信息
	 * @param pager
	 * @param qryParamMap
	 */
	public void qryDataDicByPage(Pager<DataDic> pager, Map<String, String> qryParamMap) {
		AssertUtil.isArgumentNull(pager, "分页对象不能为null");
		dataDicDAO.selectDataDicByPage(pager, qryParamMap);
	}
	
	/**
	 * 新增数据字典信息
	 * @param dataDic
	 */
	public void addDataDic(DataDic dataDic) {
		Map<String, String> qryParamMap = new HashMap<String, String>();
		qryParamMap.put("dataTp", dataDic.getDataTp());
		qryParamMap.put("dataKey", dataDic.getDataKey());
		List<DataDic> lst = dataDicDAO.selectDataDic(qryParamMap);
		if (lst != null && lst.size() > 0) {
			throw new RuntimeException("该数据字典已存在:" +
					"[dataTp=" + dataDic.getDataTp() + ",dataKey=" + dataDic.getDataKey() + "]");
		}
		dataDicDAO.addDataDic(dataDic);
	}
	
	/**
	 * 新增数据字典信息
	 * @param dataDic
	 */
	public void updateDataDic(DataDic dataDic) {
		Map<String, String> qryParamMap = new HashMap<String, String>();
		qryParamMap.put("dataTp", dataDic.getDataTp());
		qryParamMap.put("dataKey", dataDic.getDataKey());
		List<DataDic> lst = dataDicDAO.selectDataDic(qryParamMap);
		if (lst != null && lst.size() > 0) {
			throw new RuntimeException("该数据字典已存在:" +
					"[dataTp=" + dataDic.getDataTp() + ",dataKey=" + dataDic.getDataKey() + "]");
		}
		dataDicDAO.updateDataDic(dataDic);
	}
	
	/**
	 * 获取全部的数据字典信息
	 * @return
	 */
	public List<DataDic> getAllDataDic() {
		return dataDicDAO.selectDataDic(null);
	}
}
